//
//  RecommendCell.m
//  Kugou-OC
//
//  Created by BH on 16/6/2.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "RecommendCell.h"
#import "GlobalDefine.h"
#import "TapImageView.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface CollectionItemView : UIView{
    
}

@property(nonatomic,strong)TapImageView *itemImg;
@property(nonatomic,strong)UILabel *itemTitle;
@property(nonatomic,strong)UIImageView *itemLeftIcon;
@property(nonatomic,strong)UILabel *itemSubtext;
@property(nonatomic,strong)UIImageView *itemRightIcon;
@property(nonatomic,strong)NSString *leftIconName;

@end

@implementation CollectionItemView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setSubviews];
    }
    
    return self;
}

-(void)setSubviews{
    _itemImg = ALLOC(TapImageView);
    
    _itemTitle = ALLOC(UILabel);
    _itemTitle.font = TITLE_FOUR_FONT;
    _itemTitle.numberOfLines = 2;
    
    _itemLeftIcon = ALLOC(UIImageView);
    
    _itemSubtext = ALLOC(UILabel);
    _itemSubtext.font = TITLE_FOUR_FONT;
    _itemSubtext.textColor = [UIColor whiteColor];
    
    _itemRightIcon = ALLOC(UIImageView);
    _itemRightIcon.image = IMAGE(@"listen_play_normal");
    
    [self sd_addSubviews:@[_itemImg,_itemTitle]];
    [_itemImg sd_addSubviews:@[_itemLeftIcon,_itemSubtext,_itemRightIcon]];
    
    _itemImg.sd_layout.leftEqualToView(self).topEqualToView(self).rightEqualToView(self).heightIs((SCREEN_WIDTH-80)/3);
    _itemTitle.sd_layout.leftEqualToView(_itemImg).rightEqualToView(_itemImg).topSpaceToView(_itemImg,5).heightIs(40);
    _itemLeftIcon.sd_layout.leftSpaceToView(_itemImg,5).bottomSpaceToView(_itemImg,5).widthIs(13).heightEqualToWidth();
    _itemRightIcon.sd_layout.rightSpaceToView(_itemImg,0).bottomSpaceToView(_itemImg,0).widthIs(25).heightEqualToWidth();
    _itemSubtext.sd_layout.leftSpaceToView(_itemLeftIcon,5).topEqualToView(_itemLeftIcon).bottomEqualToView(_itemLeftIcon).rightSpaceToView(_itemRightIcon,5);
    
    [self setupAutoHeightWithBottomView:_itemTitle bottomMargin:5];
}

-(void)setLeftIconName:(NSString *)leftIconName{
    _leftIconName = leftIconName;
    _itemLeftIcon.image = IMAGE(_leftIconName);
}

@end

@interface RecommendCell (){
    TapImageView *std_iconImg;
    UILabel *std_titleLab;
    UILabel *std_subTitleLab;
    
    UIImageView *ms_iconImg;
    UILabel *ms_titleLab;
    UILabel *ms_firstLab;
    UILabel *ms_secondLab;
    UILabel *ms_thirdLab;
}

@end

@implementation RecommendCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
//    static NSString *identifier = @"RecommedCell";
//    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell == nil) {
//        cell = [[RecommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
    RecommendCell *cell = [[RecommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    return cell;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    
    return self;
}

-(void)setCelltype:(RecommendCellType)celltype{
    _celltype = celltype;
    
    if (celltype == kRecommendCellTypeStandard) {
        std_iconImg = ALLOC(TapImageView);
        
        std_titleLab = ALLOC(UILabel);
        std_titleLab.font = TITLE_THIRD_FONT;
        std_titleLab.text = @"123";
        
        std_subTitleLab = ALLOC(UILabel);
        std_subTitleLab.font = TITLE_FIVE_FONT;
        std_subTitleLab.textColor = TITLE_FOUR_TEXT_COLOR;
        std_subTitleLab.numberOfLines = 2;
        [self.contentView sd_addSubviews:@[std_iconImg,std_titleLab,std_subTitleLab]];
        std_subTitleLab.text = @"subtitle";
        
        std_iconImg.sd_layout.leftSpaceToView(self.contentView,15).topSpaceToView(self.contentView,5).bottomSpaceToView(self.contentView,5).widthEqualToHeight();
        std_titleLab.sd_layout.leftSpaceToView(std_iconImg,10).topSpaceToView(self.contentView,15).rightSpaceToView(self.contentView,15).heightIs(25);
        std_subTitleLab.sd_layout.leftEqualToView(std_titleLab).rightEqualToView(std_titleLab).topSpaceToView(std_titleLab,0).heightIs(40);
        
        UIImageView *playIcon = ALLOC(UIImageView);
        playIcon.image = IMAGE(@"listen_play_normal");
        [self.contentView addSubview:playIcon];
        playIcon.sd_layout.bottomEqualToView(std_iconImg).rightEqualToView(std_iconImg).widthIs(35).heightEqualToWidth();
        
        if (_indexpath.section == 5) {
            playIcon.hidden = YES;
        }
        
    }else if (celltype == kRecommendCellTypeMoreSubtitle){
        ms_iconImg = ALLOC(UIImageView);
        
        ms_titleLab = ALLOC(UILabel);
        ms_titleLab.font = TITLE_SECOND_FONT;
        
        ms_firstLab = ALLOC(UILabel);
        ms_firstLab.font = TITLE_FIVE_FONT;
        ms_firstLab.textColor = TITLE_FOUR_TEXT_COLOR;
        
        ms_secondLab = ALLOC(UILabel);
        ms_secondLab.font = TITLE_FIVE_FONT;
        ms_secondLab.textColor = TITLE_FOUR_TEXT_COLOR;
        
        ms_thirdLab = ALLOC(UILabel);
        ms_thirdLab.font = TITLE_FIVE_FONT;
        ms_thirdLab.textColor = TITLE_FOUR_TEXT_COLOR;
        
        [self.contentView sd_addSubviews:@[ms_iconImg,ms_titleLab,ms_firstLab,ms_secondLab,ms_thirdLab]];
        
        ms_iconImg.sd_layout.leftSpaceToView(self.contentView,15).topSpaceToView(self.contentView,5).bottomSpaceToView(self.contentView,5).widthEqualToHeight();
        ms_titleLab.sd_layout.leftSpaceToView(ms_iconImg,15).topSpaceToView(self.contentView,10).rightSpaceToView(self.contentView,0).autoHeightRatio(0);
        ms_firstLab.sd_layout.leftEqualToView(ms_titleLab).topSpaceToView(ms_titleLab,0).rightEqualToView(ms_titleLab).heightIs(20);
        ms_secondLab.sd_layout.leftEqualToView(ms_firstLab).topSpaceToView(ms_firstLab,0).rightEqualToView(ms_firstLab).heightIs(20);
        ms_thirdLab.sd_layout.leftEqualToView(ms_secondLab).topSpaceToView(ms_secondLab,0).rightEqualToView(ms_secondLab).heightIs(20);
        
    }else{
        if (_indexpath.section == 4) {
            [self addItemsWithColumnCount:2];
        }else{
            [self addItemsWithColumnCount:3];
        }
    }
}

-(void)addItemsWithColumnCount:(NSInteger)column{
    
    CollectionItemView *itemV = nil;
    for (int i=0; i<column; i++) {
        CollectionItemView *itemView = ALLOC(CollectionItemView);
        itemView.leftIconName = @"songsheet_earphone_img";
        if (_indexpath.section == 2) {
            RecommendModel *model = _recommendArray[i];
            [itemView.itemImg sd_setImageWithURL:[NSURL URLWithString:model.rec_model.imgUrl] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
            itemView.itemTitle.text = model.rec_title;
            itemView.itemSubtext.text = [self integerToString:model.rec_model.collectcount];
        }else if(_indexpath.section == 3){
            NSArray *temp = [_recommendArray[0] cus_special];
            RecommendCommonModel *model = temp[i];
            [itemView.itemImg sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
            itemView.itemTitle.text = model.specialname;
            itemView.itemSubtext.text = [self integerToString:model.collectcount];
        }else{
            //indexpath.section == 4
            RecommendModel *model = _recommendArray[i];
            [itemView.itemImg sd_setImageWithURL:[NSURL URLWithString:model.vli_mobilebanner] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
            itemView.itemTitle.text = model.vli_title;
            itemView.itemSubtext.text = model.vli_publishtime;
            itemV.itemRightIcon.hidden = YES;
        }
        [self.contentView addSubview:itemView];
        
        if (itemV) {
            itemView.sd_layout.leftSpaceToView(itemV,20).topSpaceToView(self.contentView,5).bottomSpaceToView(self.contentView,0).widthIs((SCREEN_WIDTH-(column+1)*20)/column);
        }else{
            itemView.sd_layout.leftSpaceToView(self.contentView,20).topSpaceToView(self.contentView,5).bottomSpaceToView(self.contentView,0).widthIs((SCREEN_WIDTH-(column+1)*20)/column);
        }
        
        itemV = itemView;
    }
}

-(void)setRecommendM:(RecommendModel *)recommendM{
    _recommendM = recommendM;
    
    if (_indexpath.section == 0) {
        [std_iconImg sd_setImageWithURL:[NSURL URLWithString:recommendM.pr_model.imgUrl] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
        std_titleLab.text = recommendM.pr_model.specialname;
        std_subTitleLab.text = recommendM.pr_model.intro;
    }else{
        [std_iconImg sd_setImageWithURL:[NSURL URLWithString:recommendM.top_model.imgUrl] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
        std_titleLab.text = recommendM.top_model.title;
        std_subTitleLab.text = recommendM.top_model.publishtime;
    }
}

-(void)setRecommendArray:(NSArray *)recommendArray{
    _recommendArray = recommendArray;
    
    if (_indexpath.section == 1) {
        if (_indexpath.row == 0) {
            ms_titleLab.text = @"新歌首发";
            [ms_iconImg sd_setImageWithURL:[NSURL URLWithString:[recommendArray[0] son_singerimgurl]] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
            ms_firstLab.text = [recommendArray[0] son_filename];
            ms_secondLab.text = [recommendArray[1] son_filename];
            ms_thirdLab.text = [recommendArray[2] son_filename];
        }else{
            ms_titleLab.text = @"新碟上架";
            [ms_iconImg sd_setImageWithURL:[NSURL URLWithString:[recommendArray[0] alb_imgurl]] placeholderImage:IMAGE(@"collectionview_placeholdimg")];
            ms_firstLab.text = [NSString stringWithFormat:@"%@-《%@》",[recommendArray[0] alb_singername],[recommendArray[0] alb_albumname]];
            ms_secondLab.text = [NSString stringWithFormat:@"%@-《%@》",[recommendArray[1] alb_singername],[recommendArray[1] alb_albumname]];
            ms_thirdLab.text = [NSString stringWithFormat:@"%@-《%@》",[recommendArray[2] alb_singername],[recommendArray[2] alb_albumname]];
        }
    }
}

//数值转换
-(NSString *)integerToString:(NSInteger)value{
    NSString *string;
    
    if (value>=10000) {
        
        string = [NSString stringWithFormat:@"%.1f万",value/10000.0];
    }else{
        string = [NSString stringWithFormat:@"%ld",value];
    }
    
    return string;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
}

@end
